package com.zpl.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpl.goods.entity.Goods;
import com.zpl.goods.entity.dto.GoodKeyWordDTO;
import com.zpl.goods.entity.vo.GoodVO;
import com.zpl.goods.entity.vo.SellVO;
import com.zpl.goods.mapper.GoodsMapper;
import com.zpl.goods.service.GoodsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public boolean insert(Goods goods) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("title",goods.getTitle());
        List<Goods> goodss = goodsMapper.selectList(queryWrapper);
        if (goodss.size()>0){
            return false;
        }
        goodsMapper.insert(goods);
        return true;
    }

    @Override
    public boolean delete(Long id) {
        if(goodsMapper.deleteById(id)>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean update(Goods goods) {
        if (goodsMapper.updateById(goods) > 0) {
            return true;
        }
        return false;
    }

    @Override
//    @Cacheable(value="goods", key="'goods_'+#id")
    public Goods getGood(Long id) {
        Goods goods = goodsMapper.selectById(id);
        return goods;
    }

    @Override
    public List<Goods> selectByTitle(String title) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.like("title",title);
        List<Goods> goodsList=goodsMapper.selectList(queryWrapper);
        return goodsList;
    }

    @Override
    public List<Goods> getAllGoods() {
        QueryWrapper queryWrapper=new QueryWrapper();
        List<Goods> goodsList=goodsMapper.selectList(queryWrapper);
        return goodsList;
    }

    @Override
    public IPage<Goods> getGoods(GoodKeyWordDTO goodKeyWordDTO) {
        return null;
    }

    @Override
    public IPage<GoodVO> getGoodList(GoodKeyWordDTO goodKeyWordDTO) {
        return null;
    }


    @Override
    public IPage<Goods> selectByPage(GoodKeyWordDTO goodKeyWordDTO) {
        Page page = new Page<>(goodKeyWordDTO.getCurrentPage(), goodKeyWordDTO.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        if(!StringUtils.isEmpty(goodKeyWordDTO.getTitle())){
            queryWrapper.like("title",goodKeyWordDTO.getTitle());
        }
        if(!StringUtils.isEmpty(goodKeyWordDTO.getCategoryId())){
            queryWrapper.eq("category_id",goodKeyWordDTO.getCategoryId());
        }
        IPage<Goods> goodsIPage = goodsMapper.selectPage(page, queryWrapper);
        return goodsIPage;
    }

    @Override
    public List<SellVO> getSellGoods() {
        List<SellVO> sellVOList=goodsMapper.getSellList();
        if(sellVOList.size()>0){
            return sellVOList;
        }
        return null;
    }

    @Override
    public List<SellVO> getNewGoods() {
        List<SellVO> sellVOS=goodsMapper.getNewList();
        if(sellVOS.size()>0){
            return sellVOS;
        }
        return null;
    }

    @Override
    public List<SellVO> getLike() {
        List<SellVO> vos=goodsMapper.getLike();
        if(vos.size()>0){
            return vos;
        }
        return null;
    }
}
